﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace AlphaSolutionsGP
{
    public partial class frmAssessmentView : Form
    {
        public Assesment theAssessment;
        private bool toModify;

        //new
        public frmAssessmentView(string modCode, int weighting)
        {
            toModify = false;
            theAssessment = new Assesment();
            InitializeComponent();
            spinnerWeightingPercentage.Maximum = weighting;
            spinnerWeightingPercentage.Value = weighting;
            fillInCombo(cmdModule, "module_Code", "MODULE");
            cmdModule.SelectedItem = modCode;
            cmdModule.Enabled = false;
            dtpOut.Value = DateTime.Now;
            dtpDue.MinDate = DateTime.Now;
        }

        ////constructor with modifiable Event
        public frmAssessmentView(Assesment e)
        {
            toModify = true;
            theAssessment = e;
            InitializeComponent();
            printData();
            cmdModule.SelectedItem = e.ModuleCode;
            cmdModule.Enabled = false;
        }

        private void printData()
        {
            fillInCombo(cmdModule, "module_Code", "MODULE");
            cmdModule.SelectedItem = theAssessment.ModuleCode;
            cmdStatus.SelectedItem = theAssessment.Status;
            dtpOut.Value = Convert.ToDateTime(theAssessment.HandOutDate);
            dtpDue.Value = Convert.ToDateTime(theAssessment.DueInDate);
            //dtpOut.Text = theAssessment.HandOutDate;
            //dtpDue.Text = theAssessment.DueInDate;
            spinnerWeightingPercentage.Value = theAssessment.WeightingPercentage;
            cmdType.SelectedItem = theAssessment.Type.ToString();
            txtDescription.Text = theAssessment.Desc;
        }

        private void btnCancel_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click_1(object sender, EventArgs e)
        {
            if (validate())
            {

                if (toModify)
                {
                    //update theAssessment with new values
                    //anEvent.EventName = txtEventName.Text;   
                    theAssessment.ModuleCode = cmdModule.SelectedItem.ToString();
                    theAssessment.Status = cmdStatus.SelectedItem.ToString();
                    theAssessment.HandOutDate = dtpDue.Text;
                    theAssessment.DueInDate = dtpOut.Text;
                    theAssessment.WeightingPercentage = (int)spinnerWeightingPercentage.Value;
                    theAssessment.Type = cmdType.SelectedItem.ToString();
                    theAssessment.Desc = txtDescription.Text;

                    //this is where an event has been modified so needs to be put back into the arraylist.
                    theAssessment.amendAssessment();
                    this.Close();

                }
                //else new assessment and add
                else
                {
                    Assesment theAssessment = new Assesment(0, cmdModule.SelectedItem.ToString(), cmdStatus.SelectedItem.ToString(), dtpOut.Text, dtpDue.Text, (int)spinnerWeightingPercentage.Value, cmdType.SelectedItem.ToString(), txtDescription.Text);
                    try
                    {
                        theAssessment.createAssessment();
                        this.Close();
                    }
                    catch
                    {
                        MessageBox.Show("Failed to add assessment.");
                    }

                }
            }
        }

        private void frmAssessmentView_Load(object sender, EventArgs e)
        {

        }
        //DINAMICALLY FILL IN A DROPDOWN MENU
        private void fillInCombo(ComboBox cb, string column, string table)
        {
            string columnValue = ""; //this will store the column value
            MySqlConnection connection = new MySqlConnection("server=194.81.104.22;" + "User Id=alpha;" + "database=alpha_db;" + "password=bravo;");
            try
            {
                connection.Open();
                MySqlCommand cmd = connection.CreateCommand();
                cmd.CommandText = "SELECT " + column + " FROM " + table + ""; //select the required column from table
                MySqlDataReader myReader = cmd.ExecuteReader(); //execute the reader
                while (myReader.Read())
                {
                    columnValue = myReader.GetString(column); //save the column value
                    cb.Items.Add(columnValue); // add the value to the combo box
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Unable to fill table due to the following error:\n\n" + ex.ToString()); //failure message
            }

            finally
            {
                connection.Close(); //close the connection

            }
        }

        private bool validate()
        {
            bool flag = true;
            string msg = "";
            if (cmdStatus.Text == "")
            {
                msg += "\n- You must select a Status for the Assessment";
                flag = false;
            }
            if (cmdType.Text == "")
            {
                msg += "\n- You must select an Assessment Type";
                flag = false;
            }

            if (txtDescription.Text == "")
            {
                msg += "\n- You must input a description for the Assessment.";
                flag = false;
            }
            if (!flag)
                MessageBox.Show(msg, "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return flag;
        }



    }
}
